package com.next.muscu.data;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;

public class ExecutionDateTemplate implements Comparable<ExecutionDateTemplate>, MyDataObject, Serializable {
	private static final String RESTDAY = ".";
	private static final String ACTIVEDAY = "X";

	private static final String PP_ID = "id";
	private static final String PP_DESCRIPTION = "description";
	private static final String PP_WEEK = "week";
	private static final String PP_DATETEMPLATE = "dateTemplate";
	private static final String PP_LEVEL = "level";

	public ExecutionDateTemplate(){
		this.description = null;
		this.week = null;
		this.dateTemplate = null;
		this.level = null;
	}

	public ExecutionDateTemplate(String Description, String Week, String DateTemplate, Level Level){
		this.setDescription(Description);
		this.setWeek(Week);
		this.dateTemplate = cleanDateTemplate(DateTemplate);
		this.level = Level;
	}

	private String cleanDateTemplate(String input){
		String result = new String();
		String restDays = "0 .";
		String legalChars = "1Xx" + restDays;

		for (Integer i=0; i<7; i++){
			if (input.length()>i){
				if (legalChars.contains(input.substring(i, i+1))){
					if (restDays.contains(input.substring(i, i+1))){
						result = result + RESTDAY;
					}
					else{
						result = result + ACTIVEDAY;
					}
				}
				else{
					result = result + RESTDAY;
				}
			}
			else{
				result = result + RESTDAY;
			}
		}
		return result;
	}


	private Integer id;
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		Integer old = this.id;

		this.id = id;
		ppChangeSupport.firePropertyChange(PP_ID, old, this.id );

	}

	private String description;
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		String old = this.description;
		this.description = description;
		ppChangeSupport.firePropertyChange(PP_DESCRIPTION, old, this.description );
	}

	private String week;
	public String getWeek() {
		return this.week;
	}

	public void setWeek(String week) {
		String old = this.week;
		this.week = week;
		ppChangeSupport.firePropertyChange(PP_WEEK, old, this.week );

	}

	private String dateTemplate;
	public String getDateTemplate() {
		return this.dateTemplate;
	}

	public void setDateTemplate(String dateTemplate) {
		String old = this.dateTemplate;
		this.dateTemplate = cleanDateTemplate(dateTemplate);
		ppChangeSupport.firePropertyChange(PP_DATETEMPLATE, old, this.dateTemplate );

	}
	
	private Level level;
	public Level getLevel() {
		return level;
	}

	public void setLevel(Level level) {
		Level old = this.level;
		
		this.level = level;
		ppChangeSupport.firePropertyChange(PP_LEVEL, old, this.level );
}
	
	// Autres methodes
	@Override
	public String toString() {
		return "ExecutionDateTemplate [id=" +id +", description=" + description + ", week=" + week + 
			", dateTemplate=" + dateTemplate + "]";
	}
	
	@Override
	 public boolean equals(Object o) {
	        if (!(o instanceof ExecutionDateTemplate))
	            return false;
	        ExecutionDateTemplate e = (ExecutionDateTemplate)o;
	        return e.getId().equals(id);
	    }
	
	@Override
    public int hashCode() {
        return id.hashCode();
    }
	
	public int compareTo(ExecutionDateTemplate e) {
        return this.getId().compareTo(e.getId());
    }
	
	public boolean copy(MyDataObject o) {
        if (!(o instanceof ExecutionDateTemplate))
            return false;
        ExecutionDateTemplate e = (ExecutionDateTemplate)o;
        this.id = e.getId();
        this.description = e.getDescription();
        this.week = e.getWeek();
        this.dateTemplate = e.getDateTemplate();
        
        return true;
	}
	
	@Override
	public void Create() {
		// TODO Auto-generated method stub

	}
	
	// Listener methods
	PropertyChangeSupport ppChangeSupport = new PropertyChangeSupport(this);

	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		// TODO Auto-generated method stub
		ppChangeSupport.addPropertyChangeListener(listener);
	}

	@Override
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		// TODO Auto-generated method stub
		ppChangeSupport.addPropertyChangeListener(propertyName, listener);
	}

	@Override
	public PropertyChangeListener[] getPropertyChangeListeners() {
		// TODO Auto-generated method stub
		return ppChangeSupport.getPropertyChangeListeners();
	}

	@Override
	public PropertyChangeListener[] getPropertyChangeListeners(
			String propertyName) {
		// TODO Auto-generated method stub
		return ppChangeSupport.getPropertyChangeListeners(propertyName);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		// TODO Auto-generated method stub
		ppChangeSupport.removePropertyChangeListener(listener);
	}

	@Override
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		// TODO Auto-generated method stub
		ppChangeSupport.removePropertyChangeListener(propertyName, listener);
	}



}
